package com.lazydong.apiforward.client.gui.util;

import cn.hutool.core.io.FileUtil;
import com.lazydong.apiforward.client.gui.enums.PathEnum;

import javax.swing.*;
import java.io.File;
import java.nio.charset.StandardCharsets;

public class DialogMsgUtil {
    public static void showDialog(String msg, String title, int type) {
        JOptionPane.showMessageDialog(null, msg, title, type);
    }

    /**
     * 引导修改hosts权限
     */
    public static void guideModifyHostsPerm() {
        Object[] options = {"引导文档"};
        // Show the option dialog
        int selectedOption = JOptionPane.showOptionDialog(
                null,
                "修改hosts文件失败，功能不可用。请允许当前用户对hosts文件有写入权限",
                "错误",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.ERROR_MESSAGE,
                null,
                options,
                options[0] // Default option (Cancel)
        );
        if (selectedOption != 0) {
            return;
        }
        String modifyHostsPermHtml = PathEnum.DESCRIPTION.path + File.separator + "modifyHostsPerm.html";
        if (!FileUtil.exist(modifyHostsPermHtml)) {
            String string = JarResFileCopyUtil.loadResourceFile("description", "modifyHostsPerm.html");
            FileUtil.writeString(string, modifyHostsPermHtml, StandardCharsets.UTF_8);
        }
        WindowsUtil.openBrowser(PathEnum.DESCRIPTION.path + File.separator + "modifyHostsPerm.html", null);
    }
}